package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.employment.aop.LogInfo;
import com.wanmait.employment.entity.Admin;
import com.wanmait.employment.entity.Industry;
import com.wanmait.employment.entity.Position;
import com.wanmait.employment.entity.PositionType;
import com.wanmait.employment.mapper.AdminMapper;
import com.wanmait.employment.mapper.IndustryMapper;
import com.wanmait.employment.mapper.PositionMapper;
import com.wanmait.employment.mapper.PositionTypeMapper;
import com.wanmait.employment.service.IndustryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class IndustryServiceImpl implements IndustryService {

    @Resource
    private IndustryMapper industryMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private PositionTypeMapper positionTypeMapper;

    @Resource
    private PositionMapper positionMapper;

    @Override
    public PageInfo<Industry> selectList(String title, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Industry> industryQueryWrapper=new QueryWrapper<>();
        industryQueryWrapper.like("title",title);
        List<Industry> industryList=industryMapper.selectList(industryQueryWrapper);
        PageInfo<Industry> industryPageInfo=new PageInfo<>(industryList);

        industryPageInfo.getList().forEach(industry -> {
            QueryWrapper<Admin> adminQueryWrapper=new QueryWrapper<>();
            adminQueryWrapper.eq("id",industry.getAdminId());
            Admin admin=adminMapper.selectOne(adminQueryWrapper);
            industry.setAdmin(admin);
        });
        return industryPageInfo;
    }
    @LogInfo("更新行业类型")
    @Override
    public void update(Industry industry) {
        industryMapper.updateById(industry);
    }

    @LogInfo("添加行业类型")
    @Override
    public Boolean insert(Industry industry) {
        LambdaQueryWrapper<Industry> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Industry::getTitle,industry.getTitle());
        Industry industry1=industryMapper.selectOne(wrapper);
        if(industry1==null){
            industry.setAdminId(1);
            industryMapper.insert(industry);
            return true;
        }else{
            return false;
        }
    }

    @LogInfo("删除行业类型")
    @Override
    public void delete(Integer id) {
        industryMapper.deleteById(id);
    }

    @Override
    public List<Industry> selectListAll() {

        return industryMapper.selectList(null);
    }

    @Override
    public List<Industry> selectAllById() {
        List<Industry> industryList=industryMapper.selectList(null);
        industryList.forEach(industry -> {
            LambdaQueryWrapper<PositionType> positionTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
            positionTypeLambdaQueryWrapper.eq(PositionType::getIndustryId,industry.getId());
            List<PositionType> positionTypeList=positionTypeMapper.selectList(positionTypeLambdaQueryWrapper);
            industry.setPositionTypeList(positionTypeList);
            positionTypeList.forEach(positionType -> {
                LambdaQueryWrapper<Position> positionLambdaQueryWrapper=new LambdaQueryWrapper<>();
                positionLambdaQueryWrapper.eq(Position::getPositionTypeId,positionType.getId());
                List<Position> positionList=positionMapper.selectList(positionLambdaQueryWrapper);
                positionType.setPositionList(positionList);
            });
        });
        return industryList;
    }
}
